//
//  HJHBaseViewController.swift
//  WeiBoPro
//
//  Created by nadyboy on 2018/4/26.
//  Copyright © 2018年 hjh. All rights reserved.
//

import UIKit

class HJHBaseViewController: UIViewController,LoginBtnDelegate {
   
    
    //用户登陆状态
    var userLogin:Bool = false
    var talebview:UITableView?
    var refresh:UIRefreshControl?
    var isPull = false//是否上拉
    var visiteorDict:[String:String]?
    
    //自定义导航条
    lazy var navgationBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: UIScreen.main.bounds.width, height: 64))
    lazy var navItem = UINavigationItem()
    //数据源方法
    @objc func loadData(){
        //如果子类没实现就关闭
        refresh?.endRefreshing()
    }
    ///代理
    func loginBtnClick(tag: Int) {
       
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.automaticallyAdjustsScrollViewInsets = false
        // Do any additional setup after loading the view.
        loadData()
        userLogin ? setUpTableView() : setUpNoLogonUI()
    }
  
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setUpNavigationBar()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
//设置界面
extension HJHBaseViewController
{
    //重写title的didset 关键一步
    override var title: String?{
        didSet{
            navItem.title = title
        }
    }
    //自定义navigationBar
   private func setUpNavigationBar() {
    view.addSubview(navgationBar)
    navgationBar.items = [navItem]
    }
    //设置tablview
    private func setUpTableView() {
        talebview = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(talebview!, belowSubview: navgationBar)
        talebview?.dataSource = self
        talebview?.delegate = self
        //缩进
        talebview?.contentInset = UIEdgeInsetsMake(navgationBar.bounds.height, 0, 0, 0)
        //添加刷新控件
        refresh = UIRefreshControl()
        refresh?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        talebview?.addSubview(refresh!)
    }
    //没有登陆
    private func setUpNoLogonUI(){
        let vc = HJHVisitorView(frame:self.view.bounds)
        vc.visitorInfo = visiteorDict
        vc.backgroundColor = UIColor.white
        vc.delegate = self
        vc.Loginblock = {(tag: Int)->()in
            
        }
        view.addSubview(vc)
    }
}
//tableviewDelegate,datasource
extension HJHBaseViewController:UITableViewDataSource,UITableViewDelegate
{
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        let section = tableView.numberOfSections - 1
        if row < 0 || section < 0 {
            return
        }
        let count = tableView.numberOfRows(inSection: section)
        if (count - 1) == row && !isPull{
            print("上拉刷新")
            isPull = true
            loadData()
        }
        
    }
}
